package com.zhuoli.service.springboot.mybatis.pagehelper.common;

import com.zhuoli.service.springboot.mybatis.pagehelper.repository.model.UserExample;
import lombok.Data;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @Author: zhuoli
 * @Date: 2018/8/16 13:31
 * @Description:
 */
@Data
public class UserExampleFactory {
    private List<FieldInfo> fieldInfoList;

    public UserExample getUserExample() {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        Class userCriteriaClazz = criteria.getClass();
        try {
            for (FieldInfo fieldInfo : fieldInfoList) {
                String methodName = "and" + fieldInfo.getFieldName() + fieldInfo.getRelation();
                Method method = userCriteriaClazz.getDeclaredMethod(methodName, fieldInfo.getFieldValue().getClass());
                method.invoke(criteria, fieldInfo.getFieldValue());
            }

        } catch (Exception e) {

        }
        return userExample;
    }

}
